<?php
// 增加用户
class ADD
{
    // 参数描述
    /*{{{*/
    /*
       +---------------------------------------------------------------+
       |输入                                                           |
       +-------------------------------------------+-------------------+
       |参数                                       |描述               |
       +-------------------------------------------+-------------------+
       |'loginname'=>账户名                        |                   |
       +-------------------------------------------+-------------------+

       +---------------------------------------------------------------+
       |输出                                                           |
       +-------------------------------------------+-------------------+
       |参数                                       |描述               |
       +-------------------------------------------+-------------------+
       |'errcode'=>SOAR_ERR::OK                    |成功               |
       +-------------------------------------------+-------------------+
     */
    /*}}}*/

    public function call($request)
    {/*{{{*/
        if(($dbconn=worker::get_dbconn('db1')) == false)
            return false;

        if(self::check_para($request) == false)
        {
            return array('errcode'=>SOAR_ERR::PARA);
        }
        $loginname=$request['loginname'];

        //    try
        //    {
        //        $stmt=$dbconn->prepare("insert into memb_info values(".
        //                "0,sysdate(),sysdate(),:loginname,:nickname,0)");
        //        $stmt->execute(array(':loginname'=>$loginname,
        //                    ':nickname'=>$loginname));
        //    }
        //    catch(PDOException $e)
        //    {
        //        if($e->errorInfo[1] != MYSQL_ERR::UNIQUE_VIOLATE)
        //        {
        //            errlog::add("%s: worker(%d) insert into memb_info fail: %s",
        //                    __METHOD__, worker::$worker_idx,
        //                    $e->getMessage()); 
        //            $response=false;
        //        }
        //        else
        //            $response=array('errcode'=>'MEMB_EXIST');
        //        return $response;
        //    }
        //    $response=array('errcode'=>SOAR_ERR::OK);

        if($dbconn->query("update seq_memb set id=last_insert_id(id+1)")
                === false)
        {
            errlog::add("%s: update seq_memb fail:%d %s",
                    __METHOD__, $dbconn->errno, $dbconn->error);
            return false;
        }
        $uuid=$dbconn->insert_id;
        $stmt=$dbconn->prepare("insert into memb_info values(".
                "?,sysdate(),sysdate(),?,?,0)");
        if($stmt === false)
        {
            errlog::add("%s: prepare insert memb_info fail:%d %s",
                    __METHOD__, $dbconn->errno, $dbconn->error);
            return false;
        }
        $stmt->bind_param('iss', $uuid, $loginname, $loginname);
        if($stmt->execute() == false)
        {
            if($stmt->errno != MYSQL_ERR::UNIQUE_VIOLATE)
            {
                errlog::add("%s: insert memb_info fail: %d %s",
                        __METHOD__, $stmt->errno, $stmt->error);
                $stmt->close();
                return false;
            }
            $response=array('errcode'=>'MEMB_EXIST');
        }
        else
        {
            $response=array('errcode'=>SOAR_ERR::OK);
        }
        $stmt->close();

        return $response;
    }/*}}}*/

    private function check_para($request)
    {/*{{{*/
        if(!isset($request['loginname']))
        {
            errlog::add("%s: loginname unset", __METHOD__);
            return false;
        }
        $loginname=$request['loginname'];
        if(strlen($loginname) > misc::NAME_SIZE)
        {
            errlog::add("%s: loginname(%s) invalid",
                    __METHOD__, $loginname);
            return false;
        }

        return true;
    }/*}}}*/
}
?>
